package com.dubious.interview.euler;

public class Problem9 {

    /**
     * A Pythagorean triplet is a set of three natural numbers, a b c, for which,
     * 
     * a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52.
     * 
     * There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product
     * abc.
     * 
     * @param args
     */
    public static void main(String[] args) {
        // TODO: We can eliminate the c variable and get that a + b + (a^2 + b^2)^(1/2) = 1000
        // brute force it with two loops

        boolean tripletFound = false;
        int a = 1;
        int b = 1;
        int c = 1;
        while (!tripletFound) {
            a++;
            for (b = 1; b <= a; b++) {
                double cDouble = Math.pow(a * a + b * b, 0.5);
                if (Math.floor(cDouble) == cDouble) {
                    c = (int) Math.floor(cDouble);
                    if (a + b + c == 1000) {
                        tripletFound = true;
                        break;
                    }
                }
            }
        }

        System.out.println("Triplet is (a,b,c) = (" + a + "," + b + "," + c + ")");
        System.out.println("With product " + (a * b * c));
    }
}
